package com.vpx.server.service.system.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vpx.server.framework.aop.ServiceHandler;
import com.vpx.server.framework.base.ResultMsg;
import com.vpx.server.framework.exception.BizException;
import com.vpx.server.bean.entity.SysMenuOperate;
import com.vpx.server.bean.info.system.SysMenuOperateInfo;
import com.vpx.server.mapper.system.SysMenuOperateMapper;
import com.vpx.server.bean.order.base.BaseDeleteOrder;
import com.vpx.server.bean.order.base.BaseIdOrder;
import com.vpx.server.bean.order.system.SysMenuOperateOrder;
import com.vpx.server.bean.order.system.SysMenuOperatePageOrder;
import com.vpx.server.bean.result.system.SysMenuOperatePageResult;
import com.vpx.server.bean.result.system.SysMenuOperateResult;
import com.vpx.server.service.system.SysMenuOperateService;
import com.vpx.server.framework.utils.DateUtils;
import com.vpx.server.utils.UserLoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liuks
 * @title: vpx
 * @module 菜单操作
 * @description: 菜单操作服务
 * @date 2021/9/9
 */
@Service
public class SysMenuOperateServiceImpl implements SysMenuOperateService {

    @Autowired
    private SysMenuOperateMapper sysMenuOperateMapper;

    @Override
    @ServiceHandler(memo = "新增菜单操作", isTransaction = true)
    public ResultMsg insert(SysMenuOperateOrder order) {
        int count = sysMenuOperateMapper.querySysMenuOperateCountByCode(order.getCode());
        if(count > 0){
            throw new BizException("菜单操作编码已存在");
        }
        SysMenuOperate menuOperate = new SysMenuOperate();
        menuOperate.from(order);
        menuOperate.setCreateUserId(UserLoginUtils.getCurrentUser().getId());
        menuOperate.setUpdateUserId(UserLoginUtils.getCurrentUser().getId());
        menuOperate.setCreateTime(DateUtils.now());
        menuOperate.setUpdateTime(DateUtils.now());
        sysMenuOperateMapper.insert(menuOperate);
        return new ResultMsg();
    }

    @Override
    @ServiceHandler(memo = "删除菜单操作", isTransaction = true)
    public ResultMsg deleteById(BaseDeleteOrder order) {
        sysMenuOperateMapper.deleteById(order.getId());
        return new ResultMsg();
    }

    @Override
    @ServiceHandler(memo = "修改菜单操作", isTransaction = true)
    public ResultMsg updateById(SysMenuOperateOrder order) {
        SysMenuOperate menuOperate = sysMenuOperateMapper.selectById(order.getId());
        if (menuOperate == null) {
            throw new BizException("未查询到菜单操作信息");
        }
        int count = sysMenuOperateMapper.querySysMenuOperateCountByCode(order.getCode());
        if(checkCodeIsExists(order)){
            throw new BizException("菜单操作编码已存在");
        }
        menuOperate.from(order);
        menuOperate.setUpdateUserId(UserLoginUtils.getCurrentUser().getId());
        menuOperate.setUpdateTime(DateUtils.now());
        sysMenuOperateMapper.updateById(menuOperate);
        return new ResultMsg();
    }

    @Override
    @ServiceHandler(memo = "查询菜单操作")
    public ResultMsg<SysMenuOperateResult> findById(BaseIdOrder order) {
        SysMenuOperate menuOperate = sysMenuOperateMapper.selectById(order.getId());
        if (menuOperate == null) {
            throw new BizException("未查询到菜单操作信息");
        }
        SysMenuOperateInfo menuOperateInfo = sysMenuOperateMapper.querySysMenuOperatebyId(order.getId());
        SysMenuOperateResult result = new SysMenuOperateResult();
        result.from(menuOperateInfo);
        return ResultMsg.success(result);
    }

    @Override
    @ServiceHandler(memo = "分页查询菜单操作")
    public ResultMsg<SysMenuOperatePageResult> pageQuery(SysMenuOperatePageOrder order) {
        if (order.getPageNum() != null && order.getPageSize() != null) {
            PageHelper.startPage(order.getPageNum(), order.getPageSize());
        }
        List<SysMenuOperateInfo> sysMenuOperateInfos = sysMenuOperateMapper.pageQuery(order);
        PageInfo<SysMenuOperateInfo> pageInfo = new PageInfo<SysMenuOperateInfo>(sysMenuOperateInfos);
        SysMenuOperatePageResult menuOperatePageResult = new SysMenuOperatePageResult();
        menuOperatePageResult.setPageInfo(pageInfo);
        return ResultMsg.success(menuOperatePageResult);
    }

    @ServiceHandler(memo = "检查菜单操作编码是否存在")
    public boolean checkCodeIsExists(SysMenuOperateOrder order){
        int count = sysMenuOperateMapper.querySysMenuOperateCountByIdAndCode(order.getId(), order.getCode());
        if(count == 1){
            return false;
        }
        int num = sysMenuOperateMapper.querySysMenuOperateCountByCode(order.getCode());
        if(num > 0){
            return true;
        }
        return false;
    }
}
